package com.hxy.manager.util;

import lombok.extern.slf4j.Slf4j;
import okio.Okio;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * @author eric
 * @description 禁止使用其他的StringUtils，如果这个类里面没有，那么可以将其他的StringUtils的方法复制过来。
 * @date 2020/3/6
 */
public class StringUtil {
    public static boolean isNull(String string) {
        return string == null || "null".equals(string);
    }

    public static boolean isEmpty(String string) {
        return string == null || "".equals(string.trim()) || "null".equals(string.trim()) || string.length() <= 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isEmptyObj(Object str) {
        return str == null || "".equals(str);
    }

    /**
     * 可以接收多个参数
     *
     * @param values
     * @return
     */
    public static boolean hasText(String... values) {
        boolean result = true;
        if (values.length > 0) {
            for (String value : values) {
                result = value != null && !"".equals(value) && value.length() > 0 && !"null".equals(value.trim());
                if (!result) {
                    break;
                }
            }
        } else {
            result = false;
        }
        return result;
    }


    public static boolean isBlank(String str) {
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    /**
     * 将文件流转成string
     *
     * @param in
     * @param charset
     * @return
     * @throws IOException
     */
    public static String inputStreamtoString(InputStream in, Charset charset) throws IOException {
        if (in == null) {
            return null;
        }
        try {
            return Okio.buffer(Okio.source(in)).readString(charset);
        } catch (IOException e) {
            throw new IOException("读取文件流到文本异常", e);
        }
    }

    public static boolean equals(String a, String b) {
        if (a == null) {
            return b == null;
        } else {
            return a.equals(b);
        }
    }

    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        } else {
            int sz = str.length();

            for (int i = 0; i < sz; ++i) {
                if (!Character.isDigit(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        }
    }

    public static boolean containsIgnoreCase(String str, String searchStr) {
        if (str != null && searchStr != null) {
            int len = searchStr.length();
            int max = str.length() - len;

            for (int i = 0; i <= max; ++i) {
                if (str.regionMatches(true, i, searchStr, 0, len)) {
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }
}
